package output;

import java.io.FileInputStream;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import regexInput.DSList;
import regexInput.DSTable;
import regexInput.TableNode;
import regexInput.Node;
import regexInput.RecursiveScan;
import regexInput.StateNode;

public class DFARun {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		JFileChooser first = new JFileChooser();
		first.showDialog(null, "Go, Find, and Open Table.dfa");	
		String output = "";
		DSList classes = null;
		DSTable table = null;
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try {
			fis = new FileInputStream(first.getSelectedFile().getAbsolutePath());
			in = new ObjectInputStream(fis);
			classes = (DSList) in.readObject();
			table = (DSTable) in.readObject();
			in.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		}
		TableDriver driver = new TableDriver(classes.getCharNodes(), table);
		
		JFileChooser opt = new JFileChooser();
		opt.showDialog(null, "Open Sample Text File To Run On");				// Sample Input in the same folder as src, you can move it to My Documents folder or change the directory in the code.
		ArrayList<String> sampleInput = null;
		try {
			sampleInput = parseTheInput(opt.getSelectedFile().getAbsolutePath());
		} catch (IOException e) {
			sampleInput = null;
			e.printStackTrace();
		}
		for(String sample : sampleInput){
			if(driver.testString(sample)){
				output += sample + "\t" + "SUCCESS" + "\n";
				System.out.println(sample + "\t" + "SUCCESS");
			}
			else{
				output += sample + "\t" + "FAIL" + "\n";
				System.out.println(sample + "\t" + "FAILED");
			}
		}
		FileWriter writer;
		writer = new FileWriter("RunResult.txt");
		writer.write("REGEX INFO\n\n" + classes.toString());
		writer.write("\nAfter running through table\n(SUCCESS = passes REGEX)\n(FAIL = did not pass REGEX)\n\n" + output);
		writer.close();
		JOptionPane.showMessageDialog(null,"Run Complete.\nOpen RunResult.txt for the results.");
	}
	
	private static ArrayList<String> parseTheInput(String path) throws IOException{
		String text;
		ArrayList<String> list = new ArrayList<String>();
		ArrayList<String> temp;
		BufferedReader in = new BufferedReader(new FileReader(path));
		
		while (in.ready()) { 
			  text = in.readLine();
			  temp = parsing(text);
			  for(String element : temp){
				  list.add(element);
			  }
			}
		return list;
	}
	
	private static ArrayList<String> parsing(String s){
		int textIndex = 0;
		char thing;
		String temp = "";
		ArrayList<String> inputList = new ArrayList<String>();
		while(textIndex < s.length()){
			thing = s.charAt(textIndex);
			if(!skipable(thing)){
				temp += thing;
			}
			else{
				if(!temp.isEmpty()){
					inputList.add(temp);
					temp = "";
				}
			}
			textIndex++;
		}
		return inputList;
	}
	
	private static boolean skipable(char a){
		if(a == ' ' ||
				a == '\t' ||
				a == '\n' ||
				a == '\r'){
				return true;
			}
			else{
				return false;
			}
	}

}
